package com.marketlive.app.b2c.struts.formatter;

import com.marketlive.system.text.PhoneNumberFormat;
import com.sourcebeat.strutslive.formatter.FormatException;
import com.sourcebeat.strutslive.formatter.Formatter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PhoneNumberFormatter extends Formatter
{
  private static Log log = LogFactory.getLog(PhoneNumberFormatter.class);
  public static final String PHONE_NUMBER_ERROR_KEY = "err.common.phoneInvalid";
  public static final int VALID_NUM_DIGITS = 10;

  public String getErrorKey()
  {
    return "err.common.phoneInvalid";
  }

  public Object convertFromPresentationFormat(String phoneNumber)
  {
    String digits = phoneNumber.replaceAll("[^0-9]", "");
    if (digits.length() != 10) {
      log.info("Invalid Phone Number entered: numnber of digits has to be equal to 10");
      throw new FormatException("Error parsing phone number: " + phoneNumber);
    }
    return digits;
  }

  public String format(String number, String countryCode)
  {
    return PhoneNumberFormat.getInstance().format(number, countryCode);
  }
}